/*
 * =====================================================================================
 *
 *       Filename:  invert_str.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  12/10/2010 12:37:37 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ken Chen (Ken), chen_kenz@emc.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#include <iostream>
#include <cassert>
#include <cstring>

using namespace std;

void do_invert_str( char* begin, char* end )
{
  for( ; begin <= end; ++begin, --end )
  {
    char tmp = *begin;
    *begin = *end; 
    *end = tmp;
  }
}

char* invert_str( char* str )
{
  assert( str != NULL );

  int len = strlen( str );
  do_invert_str( str, str + len - 1 );

  char* end = str;

  cout << str << endl;
  do{
    char* begin = end;
    while( *end != '\0' && *end != ' ' ) 
    {
      ++end;
    }

    do_invert_str( begin, end-1 );

    if( *end != '\0' )
    {
      ++end;
    }
 }while(*end);

  return str;
}


int main( int argc, char* argv[] )
{
  char str[] = "hello ";

  cout << str << endl;
  cout << invert_str( str ) << endl;

  return 0;
}
